#include "cgcolor.h"

CgColor::CgColor() : r_(0.0), g_(0.0), b_(0.0)
{
}

CgColor::CgColor(double r, double g, double b)
    : r_(r), g_(g), b_(b)
{
}

CgColor::CgColor(const CgColor &c)
    : r_(c.r()), g_(c.g()), b_(c.b())
{

}

CgColor& CgColor::operator=(const CgColor &c)
{
    if( this != &c ) {
        r_ = c.r();
        g_ = c.g();
        b_ = c.b();
    }
    return *this;
}

CgColor operator+(const CgColor &a, const CgColor &b)
{
    CgColor c(a.r() + b.r(), a.g() + b.g(), a.b() + b.b());
    c.sanitize();

    return c;
}

CgColor operator-(const CgColor &a, const CgColor &b)
{
    CgColor c(a.r() - b.r(), a.g() - b.g(), a.b() - b.b());
    c.sanitize();

    return c;
}

CgColor operator*(const CgColor &a, const CgColor &b)
{
    CgColor c(a.r() * b.r(), a.g() * b.g(), a.b() * b.b());
    return c;
}

CgColor operator*(double s, const CgColor &c)
{
    CgColor result(s*c.r(), s*c.g(), s*c.b());
    result.sanitize();

    return result;
}

void CgColor::sanitize()
{
    if( r_ < 0.0 ) r_ = 0.0;
    else if( r_ > 1.0 ) r_ = 1.0;
    if( g_ < 0.0 ) g_ = 0.0;
    else if( g_ > 1.0 ) g_ = 1.0;
    if( b_ < 0.0) r_ = 0.0;
    else if( b_ > 1.0 ) b_ = 1.0;
}
